package message

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/message"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type NotificationMessageInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 通知消息详情
func NewNotificationMessageInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NotificationMessageInfoLogic {
	return &NotificationMessageInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *NotificationMessageInfoLogic) NotificationMessageInfo(req *types.NotificationMessageInfoReq) (resp *types.NotificationMessageListDataItem, err error) {
	if req.Id == 0 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.Message.NotificationMessageInfo(l.ctx, &message.NotificationMessageInfoReq{Id: req.Id, MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "NotificationMessageInfo err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	if u.Data.Id == 0 {
		return nil, code.NotData
	}

	return &types.NotificationMessageListDataItem{
		Id:         u.Data.Id,
		MerchantId: u.Data.MerchantId,
		Language:   u.Data.Language,
		Type:       u.Data.Type,
		Title:      u.Data.Title,
		Content:    u.Data.Content,
	}, nil
}
